<?php
class InfoModel extends Model {

    protected $_validate = array(
        array('checkcode', 'checkCd', '验证码错误!', 0, 'callback'),
        array('keep', 'require', '请选择有效期!'),
        array('title', '2,40', '信息标题必须是2~40位!', 0, 'length'),
        array('title', 'illegal', '信息标题不得包含非法字符!'),
        array('title', 'checkContent', '信息标题不得包含敏感字符!', 0, 'callback', 3, array('title')),
        array('attr', 'checkAttr', '属性必须全部填写或选择!', 0, 'callback', 1),
        array('attr', 'checkAttr2', '属性必须全部填写或选择!', 0, 'callback', 2),
        array('content', '10,300', '详细内容10~300位!', 0, 'length'),
        array('content', 'checkContent', '内容包含敏感词，请重新填写!', 0, 'callback', 3, array('content')),
        array('lianxi', '2,5', '联系人必须2~5位!', 0, 'length'),
        array('phone', 'number', '联系手机必须为数字!'),
        array('phone', '11', '联系手机必须11位!', 0, 'length'),
        array('qq', 'number', '联系QQ必须为数字!'),
        array('qq', '5,12', '联系QQ必须5~12位!', 0, 'length'),
        array('email', 'email', '电子邮箱格式不正确!'),
    );

    protected $_auto = array(
        array('addtime', 'time', 1, 'function'),
        array('addip', 'getIp', 1, 'function'),
        array('content', 'setHtmlString', 3, 'function'),
        array('attr', 'createAttr', 3, 'callback'),
        array('thumbnail', 'serialize', 3, 'function'),
        array('user', 'getUser', 1, 'callback'),
        array('cid', 'getId', 1, 'callback'),
    );

    public function checkAttr() {   //用于新增验证
        $nav = M('InfoNav');
        $pid = $nav->where('id='.$_GET['id'])->getField('pid');
        $attr = $nav->where('id='.$pid)->getField('attr');
        $attrArr = explode(',', $attr);
        $attrCount = count($attrArr);
        $postCount = count($_POST['attr']);
        return $postCount == $attrCount ? true : false;
    }

    public function checkAttr2() {   //用于修改验证
        $nav = M('InfoNav');
        $info = M('Info');
        $cnav = $info->where('id='.$_GET['id'])->getField('cid');
        $pid = $nav->where('id='.$cnav)->getField('pid');
        $attr = $nav->where('id='.$pid)->getField('attr');
        $attrArr = explode(',', $attr);
        $attrCount = count($attrArr);
        $postCount = count($_POST['attr']);
        return $postCount == $attrCount ? true : false;
    }

    public function checkCd() {
        return $_POST['checkcode'] == $_SESSION['code'];
    }

    public function getUser() {
        return $_COOKIE['user'];
    }

    public function getId() {
        return $_GET['id'];
    }

    public function createAttr() {
        foreach($_POST['attr'] as $key=>$value) {
            if(is_array($value)) {
                $temp = $value;
                $value = '';
                foreach($temp as $k=>$v) {
                    $value .= $v.',';
                }
                $value = mb_substr($value, 0, -1);
            }
            $attrStr .= $key.'/'.$value.';';
        }
        $attrStr = mb_substr($attrStr, 0, -1, 'utf-8');
        return $attrStr;
    }

    public function checkContent($arg1, $arg2) {
        $input = $_POST[$arg2];
        $filterword = getSetting('filterword');
        $filterwordArr = explode(',', $filterword);
        foreach($filterwordArr as $key=>$value) {
            if(strpos($input, $value) !== false) {
                return false;
            }
        }
        return true;
    }




}

?>